#pragma once

#include "json.hpp"
#include <iostream>
#include <optional>
#include <string>

using json = nlohmann::json;

struct HardWare_t {
    std::string port;
    std::size_t baudrate;
    char parity;
    int databits;
    int stopbits;

    friend void from_json(const json &j, HardWare_t &h)
    {
        if (j.contains("baudrate")) {
            j.at("baudrate").get_to(h.baudrate);
        }

        if (j.contains("bytesize")) {
            j.at("bytesize").get_to(h.databits);
        }

        if (j.contains("stopbits")) {
            j.at("stopbits").get_to(h.stopbits);
        }

        if (j.contains("port")) {
            j.at("port").get_to(h.port);
        }

        if (j.contains("parity")) {
            std::string parity;
            j.at("parity").get_to(parity);
            parity.length() ? h.parity = parity[0] : h.parity = 'E';
        }
    }
};

enum class DeviceStat_t : uint8_t {
    DEVICE_INIT,
    DEVICE_ONLINE,
    DEVICE_OFFLINE,
};

struct Device_t {
    std::optional<std::string> uuid;
    std::optional<std::size_t> id;
    std::optional<std::string> type;
    std::optional<std::string> name;
    std::optional<std::string> model;
    HardWare_t hardware;
    DeviceStat_t _isOnline;
};
